package attributes;

import java.util.HashMap;
import java.util.Map.Entry;

import utils.TwoWayHashMap;

public class HierarchicalAttributesBundle {

	private HashMap<Integer, HierarchicalAttributes> hierarchicalAttributesSet;
	private TwoWayHashMap<Integer, String> attributeNames;
	
	public HierarchicalAttributesBundle(HashMap<Integer, HierarchicalAttributes> hierarchicalAttributesSet,
			HashMap<Integer, String> attributeNamesByIndex
	){
		this.hierarchicalAttributesSet = hierarchicalAttributesSet;
		attributeNames = new TwoWayHashMap<Integer, String>();
		for(Entry<Integer, String> entry : attributeNamesByIndex.entrySet()){
			attributeNames.put(entry.getKey(), entry.getValue());
		}
	}
	
	public HierarchicalAttributesBundle(){
		this.hierarchicalAttributesSet = new HashMap<Integer, HierarchicalAttributes>();
		attributeNames = new TwoWayHashMap<Integer, String>();
	}	
	
	public void addAttributeInfo(int index, 
			String attributeName,
			String attributeHierarchyUnparsedText
	){
		attributeNames.put(index, attributeName);
		AttributeGraphPlusDictionary attributeGraphPlusDictionary =
			HierarchicalAttributesImporter.getChildParentPairsFromString(attributeHierarchyUnparsedText);
		HierarchicalAttributes hierarchicalAttributes = 
			new HierarchicalAttributes(attributeGraphPlusDictionary);
		hierarchicalAttributesSet.put(index, hierarchicalAttributes);		
	}
	
	public static HierarchicalAttributesBundle getBundle(HashMap<Integer, String> hierarchicalAttributesFilePaths,
			HashMap<Integer, String> attributeNamesByIndex
	){
		HashMap<Integer, HierarchicalAttributes> hierarchicalAttributesSet = 
			new HashMap<Integer, HierarchicalAttributes>();
		for(Entry<Integer, String> entry : hierarchicalAttributesFilePaths.entrySet()){
			Integer i = entry.getKey();
			String path = entry.getValue();
			AttributeGraphPlusDictionary attributeGraphPlusDictionary =
				HierarchicalAttributesImporter.getChildParentPairs(path);
			HierarchicalAttributes hierarchicalAttributes = 
				new HierarchicalAttributes(attributeGraphPlusDictionary);
			hierarchicalAttributesSet.put(i, hierarchicalAttributes);
		}
		return new HierarchicalAttributesBundle(hierarchicalAttributesSet, attributeNamesByIndex);
	}	
	
	public HierarchicalAttributes getAttributeForests(Integer i){
		return hierarchicalAttributesSet.get(i);
	}
	
}
